
var Galaxytool_install_step = 1;
var nextButtonHandler = null;
var PW_mismatch_tooltip = null;

function install_tool() {
	// collect variables
	// config.php
	var dbusername = dijit.byId("username").get("value");
	var dbpassword = dijit.byId("password").get("value");
	var dbname = dijit.byId("dbname").get("value");
	var dbhost = dijit.byId("server").get("value");
	var dbprefix = dijit.byId("prefix").get("value");

	// attributes.php
	var owner = dijit.byId("toolname").get("value");
	var maxshouts = dijit.byId("maxshouts").get("value");
	var email_to = dijit.byId("email_to").get("value");
	var email_from = dijit.byId("email_from").get("value");
	var default_language = dijit.byId("step5_language").get("value");
	var global_universe = dijit.byId("universe").get("value");
	var def_to_debris = dijit.byId("def_to_debris").get("checked");
	var debris_rate = dijit.byId("debris_rate").get("value");
	var speed_rate = dijit.byId("speed_rate").get("value");
	var url = dijit.byId("url").get("value");
	
	var usePHPMailer = dijit.byId("phpmailer").get("checked");
	var SMPT_host = dijit.byId("smpt_host").get("value");
	var SMPT_user = dijit.byId("smpt_user").get("value");
	var SMPT_pass = dijit.byId("smpt_pass").get("value");
	var SMPT_secure = dijit.byId("smpt_secure").get("value");
	var SMPT_port = dijit.byId("smpt_port").get("value");
	
	// admin account details
	var admin_pass = dijit.byId("password1").get("value");

	// create config.php and attributes.php
	var xhrArgs = {
		url : "secret/ajax/ajax_install.php?step=7",
		handleAs : "json",
		content : {
			dbusername : dbusername,
			dbpassword : dbpassword,
			dbname : dbname,
			dbhost : dbhost,
			dbprefix : dbprefix,
			owner : owner,
			maxshouts : maxshouts,
			email_to : email_to,
			email_from : email_from,
			usePHPMailer : usePHPMailer,
			SMPT_host : SMPT_host,
			SMPT_user : SMPT_user,
			SMPT_pass : SMPT_pass,
			SMPT_secure : SMPT_secure,
			SMPT_port : SMPT_port,
			default_language : default_language,
			global_universe : global_universe,
			def_to_debris : def_to_debris,
			debris_rate : debris_rate,
			speed_rate : speed_rate,
			url : url,
			admin_pass: admin_pass
		},
		load : function(jsonData) {
			if (jsonData.messages != null) {
				showMessages(jsonData.messages, "./images", false);
			}

			if (jsonData.result == true) {
				dijit.byId("nextButton").set("label", TEXT_INSTALL_CONTINUE);
				dojo.disconnect(nextButtonHandler);
				nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);
			} else {

			}

		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);

}

function download_data() {
	// create config.php and attributes.php
	var xhrArgs = {
		url : "secret/ajax/ajax_install.php?step=8",
		handleAs : "json",
		load : function(jsonData) {
			if (jsonData.messages != null) {
				showMessages(jsonData.messages, "./images", false);
			}

			if (jsonData.result == true) {
				dojo.byId("step8download").innerHTML = TEXT_UPDATE_DONE;
			} else {
				dojo.byId("step8download").innerHTML = TEXT_UPDATE_FAILED;
			}

		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);
}

function validate_step2() {
	// validate step2 via ajax request and update step2 html information
	var xhrArgs = {
		url : "secret/ajax/ajax_install.php?step=2",
		handleAs : "json",
		load : function(jsonData) {
			if (jsonData.messages != null) {
				showMessages(jsonData.messages, "./images");
			}

			var failed = false;
			if (jsonData.attribute == false) {
				failed = true;
				dojo.byId("step2_attributes").className = "failure";
			} else {
				dojo.byId("step2_attributes").className = "success";
			}
			if (jsonData.config == false) {
				failed = true;
				dojo.byId("step2_config").className = "failure";
			} else {
				dojo.byId("step2_config").className = "success";
			}

			// set button to "validate" or "continue"
			if (failed == false) {
				dijit.byId("nextButton").set("label", TEXT_INSTALL_CONTINUE);
				dojo.disconnect(nextButtonHandler);
				nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);
			} else {
				dijit.byId("nextButton").set("label", TEXT_INSTALL_VALIDATE);
				dojo.disconnect(nextButtonHandler);
				nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", validate);
			}

		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);

}

function validate_step3() {
	dojo.byId("messageArea").style.display = "none";

	// first validate input fields
	var error = false;
	var fields = new Array("server", "username", "password", "dbname");
	for ( var i = 0; i < fields.length; i++) {
		if (dijit.byId(fields[i]).isValid() == false) {
			dijit.byId(fields[i]).set("state", "Error");
			error = true;
		}
	}
	if (error == true)
		return;

	// validate step3 via ajax request and update step3 html information
	var xhrArgs = {
		url : "secret/ajax/ajax_install.php?step=3",
		handleAs : "json",
		form : dojo.byId("step3_form"),
		load : function(jsonData) {
			if (jsonData.messages != null) {
				// check failed
				showMessages(jsonData.messages, "./images");
				// remain on "validate"
				// dijit.byId("nextButton").set("label",TEXT_INSTALL_VALIDATE);
				// dojo.disconnect(nextButtonHandler);
				// nextButtonHandler = dojo.connect(dijit.byId("nextButton"),
				// "onClick", validate );
			} else {
				// check succeeded
				next();
				return;
			}
		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);
}

function validate_step4() {
	dojo.byId("messageArea").style.display = "none";

	// first validate input fields
	var error = false;
	var fields = new Array("universe", "debris_rate", "speed_rate");
	for ( var i = 0; i < fields.length; i++) {
		if (dijit.byId(fields[i]).isValid() == false) {
			dijit.byId(fields[i]).set("state", "Error");
			error = true;
		}
	}
	if (error == true)
		return;

	// validate OGame URL
	var xhrArgs = {
		url : "secret/ajax/ajax_install.php?step=4",
		handleAs : "json",
		form : dojo.byId("step4_form"),
		load : function(jsonData) {
			if (jsonData.messages != null) {
				// check failed
				showMessages(jsonData.messages, "./images");
			} else {
				// check succeeded
				next();
				return;
			}
		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);
	
}

function validate_step5() {
	dojo.byId("messageArea").style.display = "none";

	// first validate input fields
	var error = false;
	var fields = new Array("toolname", "email_to", "email_from", "maxshouts");
	for ( var i = 0; i < fields.length; i++) {
		if (dijit.byId(fields[i]).isValid() == false) {
			dijit.byId(fields[i]).set("state", "Error");
			error = true;
		}
	}
	if (error == true)
		return;

	// check succeeded
	next();
}

function validate_step6() {
	dojo.byId("messageArea").style.display = "none";
	
	if (dijit.byId("password1").isValid() && (dijit.byId("password1").get("value") == dijit.byId("password2").get("value"))) {
		next();
		return;
	}
	
	dijit.byId("password1").set("state", "Error");
	PW_mismatch_tooltip = new dijit.Tooltip({
		   connectId: ["password1"],
		   label: TEXT_PW_MISMATCH
		});
}

function show_step(old_step, new_step, next_is_validate) {
	// change header of "CenterContentPane"
	dojo.byId("install_progress_title").innerHTML = TEXT_INSTALL_STEPS[new_step];
	// change step selection in progress window
	dojo.byId("progress_step" + old_step).className = "step_disabled";
	dojo.byId("progress_step" + new_step).className = "step_enabled";

	if (next_is_validate == true) {
		dijit.byId("nextButton").set("label", TEXT_INSTALL_VALIDATE);
		dojo.disconnect(nextButtonHandler);
		nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", validate);
	} else {
		dijit.byId("nextButton").set("label", TEXT_INSTALL_CONTINUE);
		dojo.disconnect(nextButtonHandler);
		nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);
	}

	Galaxytool_install_step = new_step;

	// trigger immediate check on selection for the new step
	switch (new_step) {
	case 2:
		validate_step2();
		break;
	case 7:
		install_tool();
		break;
	case 8:
		dojo.disconnect(nextButtonHandler);
		dojo.byId("nextButton").style.display = "none";
		download_data();
		break;
	}
}

function validate() {
	step = parseInt(Galaxytool_install_step);
	if (step < 2 || step > 8)
		return;

	switch (step) {
	case 2:
		validate_step2();
		break;
	case 3:
		validate_step3();
		break;
	case 4:
		validate_step4();
		break;
	case 5:
		validate_step5();
		break;
	case 6:
		validate_step6();
		break;
	}

}

// validate current step and navigate to next step
function next() {
	dijit.byId("nextButton").set("disabled", true);
	dojo.byId("messageArea").style.display = "none";
	step = parseInt(Galaxytool_install_step);
	if (step < 1 || step > 8)
		return;

	switch (step) {
	case 1:
		show_step(1, 2, true);
		break;
	case 2:
		show_step(2, 3, true);
		break;
	case 3:
		show_step(3, 4, true);
		break;
	case 4:
		show_step(4, 5, true);
		break;
	case 5:
		show_step(5, 6, true);
		break;
	case 6:
		show_step(6, 7, false);
		break;
	case 7:
		show_step(7, 8, false);
		break;
	default:
		console.log("To be implemented for step " + step);
		return;
	}

	dojo.fx.wipeOut({
		node : dojo.byId("step" + step)
	}).play();
	dojo.fx.wipeIn({
		node : dojo.byId("step" + (step + 1))
	}).play();
	dijit.byId("nextButton").set("disabled", false);
}

function check_password_strength(){
	var value = dojo.trim(dojo.byId("password1").value);
	
	if (PW_mismatch_tooltip != null) {
		PW_mismatch_tooltip.destroy();
		PW_mismatch_tooltip = null;
	}
	
	// remove password classes
	dojo.removeClass("password_strength", "password_strength_weak");
	dojo.removeClass("password_strength", "password_strength_good");
	dojo.removeClass("password_strength", "password_strength_strong");
	dojo.removeClass("password_strength", "password_strength_very_strong");
	dojo.byId("password_strength").title = "";
	if(value != "") {
		if (value.length > 5) {
			var pws = 0;

			if (value.match(/\d{1,}/))	 		   {pws++;}
			if (value.match(/[a-zäöü]{1,}/))	   {pws++;}
			if (value.match(/[A-ZÄÜÖ]{1,}/))	   {pws++;}
			if (value.match(/\W/)) 				   {pws++;}

			if((value.length > 7) && (pws > 3)) {
				dojo.addClass("password_strength", "password_strength_very_strong"); 
			} else if (pws > 2) {
				dojo.addClass("password_strength", "password_strength_strong");
			} else if (pws > 1) {
				dojo.addClass("password_strength", "password_strength_good");
			} else {
				dojo.addClass("password_strength", "password_strength_weak");
			}
		} else {
			dojo.addClass("password_strength", "password_strength_weak");
		}
	}
}

function switch_phpmailer() {
	var usePHPMailer = dijit.byId("phpmailer").get("checked");
	if (usePHPMailer == true) {
		dojo.fx.wipeIn({
			node : dojo.byId("phpmailer_details")
		}).play();
	} else {
		dojo.fx.wipeOut({
			node : dojo.byId("phpmailer_details")
		}).play();
	}
}

function galaxytool_install_init() {
	dojo.disconnect(nextButtonHandler);
	nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);	
}

dojo.ready(galaxytool_install_init);